package org.example.myleet.p708;

import org.example.myleet.Utils.Assert;

public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
        Assert.isTrue("[3,4,1,2]".equals(toString(solution.insert(parseCyclicNode(new int[]{3,4,1}), 2))));
        Assert.isTrue("[1]".equals(toString(solution.insert(parseCyclicNode(new int[]{}), 1))));
        Assert.isTrue("[1,0]".equals(toString(solution.insert(parseCyclicNode(new int[]{1}), 0))));
        Assert.isTrue("[1,1,1,1]".equals(toString(solution.insert(parseCyclicNode(new int[]{1,1,1}), 1))));
        Assert.isTrue("[3,5,0,1]".equals(toString(solution.insert(parseCyclicNode(new int[]{3,5,1}), 0))));
        Assert.isTrue("[3,5,6,1]".equals(toString(solution.insert(parseCyclicNode(new int[]{3,5,1}), 6))));
    }

    public static Node parseCyclicNode(int[] arr) {
        if (1 > arr.length) {
            return null;
        }
        Node dummy = new Node(-1);
        Node cur = dummy;
        for (int num : arr) {
            cur.next = new Node(num);
            cur = cur.next;
        }
        cur.next = dummy.next;
        return cur.next;
    }

    public static String toString(Node node) {
        StringBuilder sb = new StringBuilder();
        sb.append('[').append(node.val);
        Node cur = node.next;
        while (null != cur && cur != node) {
            sb.append(',').append(cur.val);
            cur = cur.next;
        }
        sb.append(']');
        return sb.toString();
    }
}
